package com.myf.services;

import com.myf.AppException;
import com.myf.PaginationResult;
import com.myf.Utility;
import com.myf.dtos.SelectItem;
import com.myf.dtos.warehouse.*;
import com.myf.entities.WarehouseLocationEntity;
import com.myf.mappers.WarehouseLocationMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class WarehouseLocationService {

    private final WarehouseLocationMapper warehouselocationMapper;

    public WarehouseLocationService(WarehouseLocationMapper warehouseMapper) {
        this.warehouselocationMapper = warehouseMapper;
    }

    public int save(WarehouseLocationInput input, int currentUserId) throws AppException {
        if (input.id == null || input.id == 0) {
            // 检查名称是否重复
            var map = new HashMap<String, Object>();
            map.put("warehouseId", input.warehouseId);
            map.put("name", input.name);
            if (warehouselocationMapper.isNameExists(map) == 1) {
                throw new AppException("name ".concat(input.name).concat(" already exists"));
            }
            // 插入数据库
            var entity = new WarehouseLocationEntity();
            entity.attributes = "{}";
            entity.capacityCurrent = (double) 0;
            setToEntity(input, entity);
            entity.createAt = Utility.getCurrentUTCTime();
            entity.createBy = currentUserId;
            entity.version = 0;
            return warehouselocationMapper.insert(entity);
        } else {
            // 检查名称是否重复
            var map = new HashMap<String, Object>();
            map.put("id", input.id);
            map.put("warehouseId", input.warehouseId);
            map.put("name", input.name);
            if (warehouselocationMapper.isNameExists(map) == 1) {
                throw new AppException("name ".concat(input.name).concat(" already exists"));
            }
            // 更新数据库
            var entity = this.warehouselocationMapper.selectById(input.id);
            if (entity == null) {
                throw new AppException("the warehouse location info is invalid");
            }
            setToEntity(input, entity);
            entity.updateAt = Utility.getCurrentUTCTime();
            entity.updateBy = currentUserId;
            return warehouselocationMapper.updateById(entity);
        }
    }

    private static void setToEntity(WarehouseLocationInput input, WarehouseLocationEntity entity) {
        entity.code = input.code;
        entity.name = input.name;
        entity.capacity = input.capacity;
        entity.measurementUnit = input.measurementUnit;
        entity.category = input.category;
        entity.asile = input.asile;
        entity.bay = input.bay;
        entity.row = input.row;
        entity.floor = input.floor;
        entity.x = input.x;
        entity.y = input.y;
        entity.z = input.z;
        entity.height = input.height;
        entity.width = input.width;
        entity.length = input.length;
        entity.status = input.status;
        entity.warehouseId = input.warehouseId;
    }

    public PaginationResult<WarehouseLocationQueryOutput> query(WarehouseLocationQueryInput input) {
        var map = new HashMap<String, Object>();
        if (!input.keyword.isBlank()) {
            map.put("keyword", "%".concat(input.keyword).concat("%"));
        }
        map.put("limit", input.pageSize);
        map.put("offset", input.getOffset());
        var total = warehouselocationMapper.getPaginationCount(map);
        var rows = warehouselocationMapper.getPaginationList(map);
        return new PaginationResult<>(total, rows);
    }

    public WarehouseLocationQueryOutput getById(int id) throws AppException {
        var result = warehouselocationMapper.getById(id);
        if(result.isEmpty()) {
            throw new AppException("the warehouse location is invalid");
        }
        return  result.get();
    }

    /// 删除
    public int delete(List<Integer> ids, int currentUserId) {
        var hashMap = new HashMap<String, Object>();
        hashMap.put("ids", ids);
        hashMap.put("deleteBy", currentUserId);
        hashMap.put("deleteAt", Utility.getCurrentUTCTime());
        return warehouselocationMapper.removeByIds(hashMap);
    }

    public List<SelectItem> getForSelectItems(String keyword,Integer warehouseId) {
        if (keyword != null && !keyword.isBlank()) {
            keyword = "%" + keyword + "%";
        }
        return warehouselocationMapper.getForSelectItems(keyword,warehouseId);
    }
}
